package utils

import (
	"strings"

	"github.com/beego/beego/v2/core/validation"
)

var dict = map[string]string{
	"Can not be empty":   "不能是空的",
	"Minimum size is":    "长度至少需要",
	"Maximum size is":    "长度不能超过",
	"Required length is": "长度固定是",
	"Range is":           "数值范围是",

	"Must be valid numeric characters": "必须是有效的数字",
	"Must be a valid ip address":       "必须是有效的IP地址",

	"Must match \\p{Han}+": "必须是汉字",
}

// 表单验证信息转为中文
func Translate(item *validation.Error) string {
	key := item.Key
	field := item.Field
	msg := item.Message
	msg = strings.Trim(msg, " ")
	//println("["+key+"]", "["+field+"]", "["+msg+"]")
	if field == "" {
		field = key
	}
	if !strings.HasPrefix(msg, field) {
		msg = field + msg
	}
	for k, v := range dict {
		if strings.Index(msg, k) >= 0 {
			msg = strings.Replace(msg, k, v, 1)
		}
	}
	return msg
}
